<template>
  <div class="flex flex-col h-full">
    <!-- <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="pyjm" class="w-64">
          <a-input v-model:value="formState.pyjm" placeholder="规则名称" />
        </a-form-item>
      </a-form>
    </SearchContainer> -->
    <a-card class="flex-1">
      <a-button type="primary" class="mb-2" @click="onClickAdd">新增</a-button>
      <BaseTable :loading="loading" column-code="000995" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'status'">
            <a-switch v-model:checked="record.status" @change="onChangeSwitch(record)" />
          </template>
          <template v-if="column.dataIndex === 'isRequired'">
            <span v-if="record.isRequired === 1">是</span>
            <span v-if="record.isRequired === 0">否</span>
          </template>
          <template v-if="column.dataIndex === 'qcruleType'">
            <span v-if="record.qcruleType === '1'">完整性</span>
            <span v-if="record.qcruleType === '2'">准确性</span>
            <span v-if="record.qcruleType === '3'">规范性</span>
            <span v-if="record.qcruleType === '4'">逻辑性</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="onClickAdd(record)">修改</a-button>
              <BaseButton confirm-text="是否删除?" type="text" danger size="small" @click="onClickDelete(record)"> 删除 </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
// import { post } from '@/utils/request'
// import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit'

const editOpen = ref(false)
const rowData = ref(false)
// const formRef = ref(null)
// const formState = ref({})

const { loading, tableData, pagination, getData } = useTable({
  url: '/med/MedicalRecord/getMrQcruleList',
  immediate: true,
  formatResult: (result) => {
    result.forEach((item) => {
      item.status = item.status === 1 ? true : false
    })
    return result
  }
})

const onClickAdd = (row) => {
  editOpen.value = true
  rowData.value = { ...row }
}

// 状态
const onChangeSwitch = async (row) => {
  const { code, msg } = await post('/med/MedicalRecord/doSaveMrQcrule', { ...row, status: row.status ? 1 : 0 })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
// 删除
const onClickDelete = async (row) => {
  const { code, msg } = await post('/med/MedicalRecord/delMrQcrule', { qcRuleId: row.qcRuleId })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
